package com.yangzc.lion.search.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.yangzc.lion.search.model.SearchResultBean;

/**
 *
 */
public class IndexSearchUtil {
	
	public List<SearchResultBean> getSearchResult(String searchWhich , String searchParam)
			throws IOException, ParseException, InvalidTokenOffsetsException{
		//索引所在文件夹
		File indexFile = new File("e:/lucene/index2");
		//索引位置
		Directory directory = FSDirectory.open(indexFile);
		//读取索引的indexReader
		IndexReader reader = DirectoryReader.open(directory);
		//庖丁解牛分词器(有问题，建议使用其他分词器)
		//Analyzer analyzer = new PaodingAnalyzer();
		//标准分词器
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		//指定对content还是title进行查询
		QueryParser parser = new QueryParser(Version.LUCENE_43, searchWhich, analyzer);
		//创建indexSearcher
		IndexSearcher searcher = new IndexSearcher(reader);
		//对用户的输入进行查询
		Query query = parser.parse(searchParam);
		//根据date字段进行排序，得到查询结果
		ScoreDoc[] scoreDocs = searcher.search(query, 1000).scoreDocs;
		//创建list，将结果保存其中，以便在jsp页面中进行显示
		List<SearchResultBean> list = new ArrayList<SearchResultBean>();
		//ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		ScoreDoc hit = null;
		for (int i = 0; i < scoreDocs.length; i++) {
			hit = scoreDocs[i];
			Document doc = searcher.doc(hit.doc);
			//取得该条索引文档
			SearchResultBean srb = new SearchResultBean();
			//从中取出标题
			String title = doc.get("fd_name");
			//从中取出内容
			String content = doc.get("fd_login_name");
			//从中取出主键id
			String id = doc.get("fd_id");
			//从中取出发布时间
			String date = doc.get("fd_create_time");
			//高亮htmlFormatter对象
			SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
			//高亮对象
			Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(query));
			//设置高亮附近的字数
			highlighter.setTextFragmenter(new SimpleFragmenter(100));
			//如果查询的是标题，进行处理
			if(searchWhich.equals("fd_name")) {
				String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,title);
				//获得高亮后的标题内容
				srb.setTitle(bestFragment);
				//如果内容不足150个字，全部设置
				if(content.length() < 150) {
					srb.setContent(content);
				}else {
					//如果内容多于150个字，只取出前面150个字
					srb.setContent(content.substring(0 , 150));
				}
			} else {
				//如果查询的是内容字段
				String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,content);
				//取得高亮内容并设置
				srb.setContent(bestFragment);
				//设置标题，全部设置
				srb.setTitle(title);
			}
			//设置日期
			srb.setDate(date);
			//设置主键
			srb.setId(id);
			//添加到list中，以便在jsp页面上显示
			list.add(srb);
		}
		return list;
	}
	
	public static void main(String[] args) throws Exception{
		IndexSearchUtil isu = new IndexSearchUtil();
		List list = isu.getSearchResult("title", "电脑");
		System.out.println("搜索结果： " + list.size());
	}
}
